package bma.common.langutil.concurrent;

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicSeq {
	protected AtomicInteger atomValue;
	protected int maxValue;
	protected int resetValue;

	public AtomicSeq() {
		this(Integer.MAX_VALUE - 10000, 0);
	}

	public AtomicSeq(int maxv) {
		this(maxv, 0);
	}

	public AtomicSeq(int maxv, int resetv) {
		super();
		atomValue = new AtomicInteger(resetv);
		maxValue = maxv;
		resetValue = resetv;
	}

	public int next() {
		int v = atomValue.incrementAndGet();
		while (v > maxValue) {
			atomValue.compareAndSet(v, resetValue);
			v = atomValue.incrementAndGet();			
		}
		return v;
	}
}
